<?php

require_once './application/core/CoreBase.php';
require_once './application/core/Client/configSoap.php';
require_once './application/models/Tables/cache.php';
require_once './application/models/Objets/Composant.php';
require_once './application/entity/config/datalist.php';
require_once 'XML/xmlToObjArray.php';

class backupCore extends CoreBase
{

    private $_identity;
    private $_ip;
    private $_model;
    private $_type;
    private $_object;
    private $_id;
    private $_proto;
    private $_objXml;
    private $_objCompo;

    public function __construct(&$controller)
    {
        $this->_controller = $controller;
        $this->_session = new Zend_Session_Namespace('session');
        $this->_session->backup = 0;
    }

    public function backup()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_model = $this->_controller->getRequest()->getParam('model');
        $this->_type = $this->_controller->getRequest()->getParam('type');
        $this->_identity = $this->_controller->getRequest()->getParam('identity');
        $this->_proto = $this->_controller->getRequest()->getParam('proto');
        if (stristr($this->_proto, "auto"))
            $this->_proto = "ssh,telnet";
        $obj = new CoreBase($this->_controller);
        $this->_identity = $obj->getUsernameBase($this->_identity);
        $this->_session->idSession = $this->_controller->_idClient;
        $this->_ip = explode(',', $this->_ip);
        foreach ($this->_ip as $ip)
        {
            $this->_session->backup++;
            $ip = $obj->parseArrayIp($ip);
            $params = array('Ip' => $ip,
                'Service' => 'backup',
                'Model' => $this->_model,
                'Type' => $this->_type,
                'Name' => 'backup',
                'Identity' => $this->_identity,
                'Protocol' => $this->_proto);
            $this->_object = new configSoap($this->_controller);
            $this->_object->backup($params, 'backupresp');
        }
        $this->MiseEnAttenteReponse(true, '/config/checkbackupresp', $this->_session->backup);
    }

    public function checkBackupResp()
    {
        $this->_data = $this->_controller->getRequest()->getParam('donnees');
        $cache = new cache();
        $rst = $cache->getInfo($this->_controller->_idClient, "backupresp");
        if ($rst != false)
        {
            foreach ($rst as $key => $val)
            {
                $this->_data--;
                $tmp = explode(';', $rst[$key]['Xml']);
                if ($tmp[0] == 'error')
                    $this->_session->backupResp .= 'false;' . $tmp[1] . ',';
                else
                    $this->_session->backupResp .= 'true;' . $tmp[1] . ',';
                $cache->deleteInfo($this->_controller->_idClient, 'backupresp', $rst[$key]['Id']);
            }
            if ($this->_data == 0)
            {
                $this->MiseEnAttenteReponse(false, '/config/checkbackupresp', $this->_session->backupResp);
                $this->_session->backupResp = null;
            }
            else
                $this->MiseEnAttenteReponse(true, '/config/checkbackupresp', $this->_data);
        }
        else
            $this->MiseEnAttenteReponse(true, '/config/checkbackupresp', $this->_data);
    }

    public function backupResp($xml, $idClient)
    {
        $cache = new cache();
        $this->_objXml = new xmlToObjArray();
        $this->_objCompo = new ObjectComposant();
        $this->_data = $this->_objXml->parse_xml_data($xml, false);
        $this->_ip = $this->_data[0]['PARAM'][0]['BACKUP'][0]['IP'][0][0];
        $this->_id = $this->_objCompo->rechercheComposant($this->_ip);
        if (empty($this->_data[0]['PARAM'][0]['BACKUP'][0]['DATA'][0]))
        {
            $cache->writeInfo($idClient, "backupresp", "error;" . $this->_id[0]['HostName']);
            return false;
        }
        $this->_data = base64_decode($this->_data[0]['PARAM'][0]['BACKUP'][0]['DATA'][0][0]);
        $this->_objCompo->ajouterConfiguration($this->_id[0]['IdComposant'], $this->_data);
        $cache->writeInfo($idClient, "backupresp", "end;" . $this->_id[0]['HostName']);
        return true;
    }

//envoie la list de user pour les backup
    public function getListUserBase()
    {
        $obj = new CoreBase($this->_controller);
        $this->_identity = $obj->listIdentityFilter();
        $this->_param = $obj->listParamFilter();
        $obj = new dataList();
        $obj->model = $this->_param['model'];
        $obj->type = $this->_param['type'];
        $obj->protocol = $this->_param['protocol'];
        $obj->identity = $this->_identity;
        echo Zend_Json_Encoder::encode($obj);
        return true;
    }

}
?>
